package com.example.main;

import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.security.MessageDigest;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.net.util.Base64;

import com.example.rmi.remote.IHelloService;

public class MainExample {
	
	public static void main(String... args) throws Exception{
		int[] values = {3, 10, 2, 5, 2, 4};
		
		//System.out.println(getMaxValue(values));
		System.out.println(decrypt("cmn8nCa3nzUDaYPm2zSbCw=="));
		/*
		int endIndex = 0;
		final String value = "Mauricio Salazar Cervantes";
		
		System.out.println(fib(10));
		//printNumber(10);
		
		
		endIndex = value.length() - 1;
		for (int x = value.length() - 1; x >= 0; x--){
			
			if (value.charAt(x) == ' '){
				
				for (int y = x; y <= endIndex; y++){
					System.out.print(value.charAt(y));
				}
				
				endIndex = x;
			}
		}
		
		for (int x = 0; x < endIndex; x++){
			System.out.print(value.charAt(x));
		}
		
		//rmiMethod();
		 * 
		 */
	}
	
	public static void rmiMethod() throws Exception{
		Registry registry = null;
		IHelloService helloService = null;
		
		registry = LocateRegistry.getRegistry("localhost", 7001);
		helloService = (IHelloService) registry.lookup("helloService");
		
		System.out.println(helloService.getHelloMessage());
		helloService = (IHelloService) Naming.lookup("//localhost:7001/helloService");
		
		System.out.println(helloService.getHelloMessage());
	}
	
	public static void printNumber(int number){
		
		if (number > 0){
			printNumber(--number);
		}
		
		System.out.println(number);
	}
	
	public static int fib(int number){
		if (number <= 1){
			return number;
		} else {
			System.out.println((number - 1) + " " + (number - 2));
			return fib(number -1) + fib(number -2);
		}
	}
	
	public static int getMaxValue(int[] listValue){
		int maxValue = 0;
		
		if (listValue != null){
			maxValue = listValue[0];
			
			for (int x = 0; x < listValue.length - 1; x++){
				
				if (listValue[x] > maxValue){
					maxValue = listValue[x];
				}
			}
		}
		
		return maxValue;
	}
	
	public static String decrypt(String encrypted) throws Exception{
        Cipher c = Cipher.getInstance("AES");
        String key = "`uV;s7,il,#ZLb\\oTHD%r0|#[wlGo785@2y9Uq4k.rbi.C9$Qho*T=/WM)5>hl^";
        byte[] keyBytes = MessageDigest.getInstance("SHA-1").digest(("s4lt184ggs"+key).getBytes());
        c.init(Cipher.DECRYPT_MODE, new SecretKeySpec(keyBytes, 0, 16, "AES"));
        
        return new String(c.doFinal(new Base64().decode(encrypted.getBytes())));
	}
}